package com.gravata.enigma;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;

import com.gravata.enigma.reader.ParseFavoritos;
import com.gravata.enigma.reader.ParseLameDb;
import com.gravata.enigma.reader.ParseSatelites;

public class LoadFromReceiver extends AsyncTask<String, String, Boolean> {

		private OnTaskCompleted tContext;
		private Context context;
		private ProgressDialog progress;

		public LoadFromReceiver(OnTaskCompleted context) {
			this.tContext=context;
			this.context=(Context) context;
		}
		public LoadFromReceiver(Context context, OnTaskCompleted tContext){
			this.context=context;
			this.tContext=tContext;
		}

		@Override
		protected Boolean doInBackground(String... arg0) {
			ParseSatelites.parse(context,publish);
			ParseLameDb.parse(context,publish);
			ParseFavoritos.parse(context,publish);

		return true;
	}

	  @Override
	  protected void onPreExecute() {
		  progress = new ProgressDialog((Activity) context);
	      //Cria novo um ProgressDialogo e exibe
	      progress.setMessage(context.getString(R.string.ftp_conectando)+"...");
	      progress.setCancelable(false);
	      progress.show();
	  }
	   @Override
	      protected void onProgressUpdate(String... values) {
	          //Atualiza mensagem
	    	  if(progress!=null)
	    		  progress.setMessage(values[0]);
	      }

	   @Override
	      protected void onPostExecute(Boolean result) {
		   if(!result){
			   try {
				Thread.sleep(4000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		   }else
			   tContext.onTaskCompleted();
		   progress.dismiss();
	   }

	   private PublishMessage publish=new PublishMessage() {

		@Override
		public void publish(String mensagem) {
			publishProgress(mensagem);
		}
	};
}